package net.cooltain.warhammer.running.window.panel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPopupMenu;
import javax.swing.border.Border;

import sun.java2d.loops.DrawRect;

import net.cooltain.utils.Logger;
import net.cooltain.warhammer.framework.constant.ConstantCardType;
import net.cooltain.warhammer.framework.constant.ConstantPicture;
import net.cooltain.warhammer.framework.constant.ConstantPlayZone;
import net.cooltain.warhammer.running.entity.MotionCard;
import net.cooltain.warhammer.running.entity.MotionPlayerZone;
import net.cooltain.warhammer.running.window.MainFrame;

public class CardLabel extends JLabel implements MouseListener {
	
	private static final long serialVersionUID = -4591665403799347308L;
	private static Border emptyBorder = BorderFactory.createEmptyBorder();
	private static Border mouseOnBorder = BorderFactory.createLineBorder(Color.green, 2);
	
	protected MotionPlayerZone actPlayer ; // 显示这个卡片的玩家,不一定是拥有卡片的人
	protected MotionCard motionCard ;   // 卡片
	
	protected static BufferedImage backImage = null;
	
	
	public CardLabel(MotionPlayerZone actPlayer,MotionCard motionCard) {
		super();
		this.setDoubleBuffered(true);
		this.motionCard = motionCard;
		this.actPlayer = actPlayer;
		if(backImage==null){
			try {
				backImage = ImageIO.read(new File(ConstantPicture.PIC_CARD_BACK));
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	
	public void paintComponent(Graphics g){ 
		super.paintComponent(g); 
		try {
			if(motionCard.isFlip()){
				g.drawImage(backImage, 0, 0, MainFrame.CARD_W, MainFrame.CARD_H, null);				
			}else if(motionCard.checkVisiblePlayer(actPlayer)){
				g.drawImage(motionCard.cardEntity.getIconImage().getImage(),0,0, MainFrame.CARD_W,MainFrame.CARD_H,   null);				
			}else{
				g.drawImage(backImage, 0, 0, MainFrame.CARD_W, MainFrame.CARD_H, null);				
			}
			
			if( this.motionCard.isFlip()){
				if(this.motionCard.isDevelopment()){
					g.setColor(Color.WHITE);
					g.drawString("设施", 4, 14);
				}
			}else{
				if( this.motionCard.cardEntity.getType()==ConstantCardType.CARD_TYPE_UNIT){
					if(ConstantPlayZone.normalCardAllowZone.containsKey(this.motionCard.currentZone)){
						g.setColor(Color.LIGHT_GRAY);
						g.fill3DRect(5, this.getHeight()-15, this.getWidth()-10, 15 , true);
						g.setColor(Color.BLUE);
						String str = this.motionCard.getPowerSum() 
						+ " / "
						+ this.motionCard.getHealthSum() ;
						g.drawString(str, 10, this.getHeight()-3);
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void mouseClicked(MouseEvent e) {
		int mods=e.getModifiers();
		MainFrame mainFrame = actPlayer.mainFrame;
//		Logger.debug(actPlayer.playerName+" 选中了 "+ this.motionCard.cardEntity.getName());
		if( !this.motionCard.equals( mainFrame.previewCard) ){
			mainFrame.previewCard = motionCard;
			mainFrame.reloadPreview();
		}
		if((mods & InputEvent.BUTTON3_MASK)!=0){
			JPopupMenu jpopup = motionCard.cardEntity.genePopupMenu(actPlayer);
			if(jpopup!=null){
				jpopup.show(this, e.getX() , e.getY());
			}
		}
	}
	
	public void mouseEntered(MouseEvent e) {
		this.setBorder(mouseOnBorder);
	}
	
	public void mouseExited(MouseEvent e) {
		this.setBorder(emptyBorder);
	}
	
	public void mousePressed(MouseEvent e) {
		
	}
	
	public void mouseReleased(MouseEvent e) {
		
	}

}
